package edu.sfsu.powerrangers.jeopardy.editor;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import edu.sfsu.powerrangers.jeopardy.gamestate.Category;
import edu.sfsu.powerrangers.jeopardy.gamestate.Round;

public class RoundPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private ValuesPanel valuesPanel;
	private JPanel categoryPanel;
	
	public RoundPanel(int rows, int cols) {
		super(new BorderLayout(4,4));
		valuesPanel = new ValuesPanel(rows);
		categoryPanel = new JPanel(new GridLayout(1,0,4,4));
		while (cols-->0) {
			categoryPanel.add(new CategoryPanel(rows));
		}
		
		JButton addCatBtn = new JButton("Add Category");
		addCatBtn.putClientProperty("JComponent.sizeVariant", "mini");
		addCatBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setCategoryCount(getCategoryCount()+1);
			}
		});
		JButton remCatBtn = new JButton("Remove Category");
		remCatBtn.putClientProperty("JComponent.sizeVariant", "mini");
		remCatBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (getCategoryCount() > 1) {
					setCategoryCount(getCategoryCount()-1);
				}
			}
		});
		JButton addRowBtn = new JButton("Add Row");
		addRowBtn.putClientProperty("JComponent.sizeVariant", "mini");
		addRowBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setRowCount(getRowCount()+1);
			}
		});
		JButton remRowBtn = new JButton("Remove Row");
		remRowBtn.putClientProperty("JComponent.sizeVariant", "mini");
		remRowBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (getRowCount() > 1) {
					setRowCount(getRowCount()-1);
				}
			}
		});
		JPanel buttonPanel1 = new JPanel(new GridLayout(1,0,4,4));
		buttonPanel1.add(addCatBtn);
		buttonPanel1.add(remCatBtn);
		buttonPanel1.add(addRowBtn);
		buttonPanel1.add(remRowBtn);
		buttonPanel1.setMaximumSize(buttonPanel1.getPreferredSize());
		JPanel buttonPanel2 = new JPanel();
		buttonPanel2.setLayout(new BoxLayout(buttonPanel2, BoxLayout.X_AXIS));
		buttonPanel2.add(Box.createHorizontalGlue());
		buttonPanel2.add(buttonPanel1);
		buttonPanel2.add(Box.createHorizontalGlue());
		
		add(valuesPanel, BorderLayout.LINE_START);
		add(categoryPanel, BorderLayout.CENTER);
		add(buttonPanel2, BorderLayout.PAGE_END);
	}
	
	public int getCategoryCount() {
		return categoryPanel.getComponentCount();
	}
	
	public CategoryPanel getCategory(int i) {
		return (CategoryPanel)categoryPanel.getComponent(i);
	}
	
	public void addCategory(CategoryPanel category) {
		categoryPanel.add(category);
		categoryPanel.validate();
	}
	
	public void addCategory(int index, CategoryPanel category) {
		categoryPanel.add(category, index);
		categoryPanel.validate();
	}
	
	public void addCategory(CategoryPanel category, int index) {
		categoryPanel.add(category, index);
		categoryPanel.validate();
	}
	
	public void removeCategory(int index) {
		categoryPanel.remove(index);
		categoryPanel.validate();
	}
	
	public void removeCategory(CategoryPanel category) {
		categoryPanel.remove(category);
		categoryPanel.validate();
	}
	
	public void moveCategoryLeft(int index) {
		CategoryPanel category = (CategoryPanel)categoryPanel.getComponent(index);
		categoryPanel.remove(index);
		index--;
		if (index < 0) {
			index = 0;
		}
		categoryPanel.add(category, index);
		categoryPanel.validate();
	}
	
	public void moveCategoryRight(int index) {
		CategoryPanel category = (CategoryPanel)categoryPanel.getComponent(index);
		categoryPanel.remove(index);
		index++;
		if (index > categoryPanel.getComponentCount()) {
			index = categoryPanel.getComponentCount();
		}
		categoryPanel.add(category, index);
		categoryPanel.validate();
	}
	
	public void setCategoryCount(int count) {
		while (categoryPanel.getComponentCount() > count) {
			categoryPanel.remove(count);
		}
		while (categoryPanel.getComponentCount() < count) {
			categoryPanel.add(new CategoryPanel(getRowCount()));
		}
		categoryPanel.validate();
	}
	
	public int getRowCount() {
		return valuesPanel.getValueCount();
	}
	
	public void moveRowUp(int index) {
		valuesPanel.moveValueUp(index);
		for (Component c : categoryPanel.getComponents()) {
			((CategoryPanel)c).moveClueUp(index);
		}
	}
	
	public void moveRowDown(int index) {
		valuesPanel.moveValueDown(index);
		for (Component c : categoryPanel.getComponents()) {
			((CategoryPanel)c).moveClueDown(index);
		}
	}
	
	public void setRowCount(int count) {
		valuesPanel.setValueCount(count);
		for (Component c : categoryPanel.getComponents()) {
			((CategoryPanel)c).setClueCount(count);
		}
	}
	
	public Round getRound() {
		Round round = new Round();
		List<Integer> values = valuesPanel.getValues();
		for (Component c : categoryPanel.getComponents()) {
			round.add(((CategoryPanel)c).getCategory(values));
		}
		return round;
	}
	
	public void setRound(Round round) {
		List<Integer> values = new Vector<Integer>();
		categoryPanel.removeAll();
		for (Category c : round) {
			CategoryPanel cp = new CategoryPanel(0);
			cp.setCategory(c);
			categoryPanel.add(cp);
			if (c.size() > values.size()) {
				for (int i = values.size(); i < c.size(); i++) {
					values.add(c.get(i).getValue());
				}
			}
		}
		valuesPanel.setValues(values);
		for (Component c : categoryPanel.getComponents()) {
			((CategoryPanel)c).setClueCount(values.size());
		}
	}
}
